/*
 * Filename:    CostConstansts.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月16日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.paic.mhis.hcpms.quota.settlement.dto.CostCoverDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostDataDTO;
import com.pingan.um.client.util.StringUtil;

public class CostConstansts {
	public static final String medicalIndciationCode = "KPI0055"; // 药费占比
	public static final String opertionIndicationCode = "KPI0009";// 手术费占比
	public static final String checkIndicationCode = "KPI0008";// 检查化验费占比

	public static final String medicalOrgType = "medical_org_type";// 医院类型
	public static final String medicalOrgLabel = "medical_org_level";// 医院级别

	public static final String sociationCode = "G002"; // 统筹区编码

	public static final String ALLDATA = "-1"; // 全部

	/**
	 * 获取查询数据的时间维度
	 * 
	 * @return
	 */
	public static List<String> getQueryTimeRang(String endDate) {
		String[] date = endDate.split("-");
		List<String> list = new ArrayList<String>();

		if (StringUtil.isNotEmpty(date[1])) {
			// int numMonth = Integer.valueOf(date[1]);
			// 当前月份向前推一年
			int year = Integer.parseInt(date[0]) - 1;
			int month = Integer.parseInt(date[1]) + 1;
			if (month > 12) {
				year = year + 1;
				month = 1;
			}
			boolean isFlag = true;
			for (int i = 1; i <= 12; i++) {
				if (isFlag && month > 12) {
					year = year + 1;
					month = 1;
					isFlag = false;
				}
				if (month < 10) {
					list.add(year + "-0" + month);
				} else {
					list.add(year + "-" + month);
				}
				month++;
			}
		}
		return list;
	}

	/**
	 * 获取指标维度
	 * 
	 * @return
	 */
	public static List<String> getKpiCodeList() {
		List<String> kpiCodeList = new ArrayList<String>();
		kpiCodeList.add(CostConstansts.medicalIndciationCode);
		kpiCodeList.add(CostConstansts.opertionIndicationCode);
		kpiCodeList.add(CostConstansts.checkIndicationCode);
		return kpiCodeList;

	}

	public static List<BigDecimal> createSetListValue(int listSize) {
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		for (int i = 0; i < listSize; i++) {
			list.add(i, new BigDecimal(0.00));
		}
		return list;
	}

	public static void main(String[] args) {
		// DecimalFormat df = new DecimalFormat("#####0.00");
		//
		// double d1 = 333.23456;
		// double d2 = 0.0;
		// double d3 = 2.0;
		// df.format(d1);
		// String d = df.format(d2);
		// df.format(d3);
		// System.out.println(d);
		// System.out.println(d2);
		// System.out.println(d3);
		List<BigDecimal> otherPerList = new ArrayList<BigDecimal>();
		BigDecimal a = new BigDecimal(1.2).setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal b = new BigDecimal(1.2).setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal c = new BigDecimal(1.2).setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal d = new BigDecimal(1.2).setScale(2, BigDecimal.ROUND_HALF_UP);
		otherPerList.add(a);
		otherPerList.add(b);
		otherPerList.add(c);
		otherPerList.add(d);
		BigDecimal[] other = otherPerList.toArray(new BigDecimal[0]);
		System.out.println(Arrays.toString(other));

	}

	/**
	 * 获取每个月份的各项指标的值 List<HashMap<Integer, CostCoverDTO>> listMedical
	 * 
	 * @param listMedical
	 * @param listOpertion
	 * @param listCheck
	 * @return
	 */
	public static CostDataDTO getCostDataDTO(List<HashMap<Integer, CostCoverDTO>> listMedical,
			List<HashMap<Integer, CostCoverDTO>> listOpertion, List<HashMap<Integer, CostCoverDTO>> listCheck,
			String endDate, Map<String, Integer> orderMap) {

		CostDataDTO renturnBean = new CostDataDTO();
		List<String> monthRang = CostConstansts.getQueryTimeRang(endDate); // 获取时间范围

		List<CostCoverDTO> costCoverVOList = new ArrayList<CostCoverDTO>();

		List<BigDecimal> medicalPerList = createSetListValue(orderMap.size());// new
																				// ArrayList<Double>();
		List<BigDecimal> opertionPerList = createSetListValue(orderMap.size());// new
																				// ArrayList<Double>();
		List<BigDecimal> checkPerList = createSetListValue(orderMap.size());// new
		// ArrayList<Double>();
		List<BigDecimal> otherPerList = new ArrayList<BigDecimal>();

		for (String month : monthRang) {

			CostCoverDTO costCoverVO = new CostCoverDTO();
			costCoverVO.setDate(month);

			// /获取当前月份的医药占比

			for (HashMap<Integer, CostCoverDTO> map : listMedical) {

				Iterator<Entry<Integer, CostCoverDTO>> iterator = map.entrySet().iterator();
				while (iterator.hasNext()) {
					Entry<Integer, CostCoverDTO> currentEntry = iterator.next();

					CostCoverDTO medicalBean = currentEntry.getValue();
					Integer currentIndex = currentEntry.getKey();

					String medicalMonth = medicalBean.getDate();
					BigDecimal medicalValue = medicalBean.getMedicalPer();

					if (month.equals(medicalMonth)) {

						if (medicalValue != null) {
							BigDecimal value = medicalValue.setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setMedicalPer(value);
							medicalPerList.add(currentIndex, value);
						} else {
							BigDecimal value = new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setMedicalPer(value);
							medicalPerList.add(currentIndex, value);
						}

						break;
					}

				}
			}

			/*
			 * for (CostCoverDTO medicalBean : listMedical) { String
			 * medicalMonth = medicalBean.getDate(); BigDecimal medicalValue =
			 * medicalBean.getMedicalPer();
			 * 
			 * if (month.equals(medicalMonth)) {
			 * 
			 * if (medicalValue != null) {
			 * costCoverVO.setMedicalPer(medicalValue);
			 * medicalPerList.add(orderMap.get(medicalMonth),
			 * medicalValue.doubleValue()); } else {
			 * costCoverVO.setMedicalPer(new BigDecimal(0));
			 * medicalPerList.add(orderMap.get(medicalMonth), 0d); }
			 * 
			 * break; } }
			 */

			// /获取当前月份的手术费占比

			for (HashMap<Integer, CostCoverDTO> map : listOpertion) {

				// Set<Integer> set = map.keySet();
				// Iterator<Integer> iterator = set.iterator();
				Iterator<Entry<Integer, CostCoverDTO>> entryKeyIterator = map.entrySet().iterator();

				while (entryKeyIterator.hasNext()) {
					Entry<Integer, CostCoverDTO> e = entryKeyIterator.next();
					CostCoverDTO opertionBean = e.getValue();
					Integer currentIndex = e.getKey();
					// CostCoverDTO opertionBean = map.get(currentIndex);

					String opertionMonth = opertionBean.getDate();
					BigDecimal opertionValue = opertionBean.getOperationPer();

					if (month.equals(opertionMonth)) {

						if (opertionValue != null) {
							BigDecimal value = opertionValue.setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setOperationPer(value);
							opertionPerList.add(currentIndex, value);
						} else {
							BigDecimal value = new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setOperationPer(new BigDecimal(0));
							opertionPerList.add(currentIndex, value);
						}

						break;
					}
				}
			}

			/*
			 * for (CostCoverDTO opertionBean : listOpertion) { String
			 * opertionMonth = opertionBean.getDate(); BigDecimal opertionValue
			 * = opertionBean.getOperationPer();
			 * 
			 * if (month.equals(opertionMonth)) {
			 * 
			 * if (opertionValue != null) {
			 * costCoverVO.setOperationPer(opertionValue);
			 * opertionPerList.add(orderMap.get(opertionMonth),
			 * opertionValue.doubleValue()); } else {
			 * costCoverVO.setOperationPer(new BigDecimal(0));
			 * opertionPerList.add(orderMap.get(opertionMonth), 0d); }
			 * 
			 * break; } }
			 */

			// /获取当前月份的化验占比

			for (HashMap<Integer, CostCoverDTO> map : listCheck) {

				// Set<Integer> set = map.keySet();
				// Iterator<Integer> iterator = set.iterator();
				Iterator<Entry<Integer, CostCoverDTO>> entryKeyIterator = map.entrySet().iterator();

				while (entryKeyIterator.hasNext()) {
					Entry<Integer, CostCoverDTO> e = entryKeyIterator.next();
					CostCoverDTO checkBean = e.getValue();
					Integer currentIndex = e.getKey();
					// Integer currentIndex = iterator.next();
					// CostCoverDTO checkBean = map.get(currentIndex);

					String checkMonth = checkBean.getDate();
					BigDecimal checkValue = checkBean.getCheckPer();

					if (month.equals(checkMonth)) {

						if (null != checkValue) {
							BigDecimal value = checkValue.setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setCheckPer(value);
							checkPerList.add(currentIndex, value);

						} else {
							BigDecimal value = new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP);
							costCoverVO.setCheckPer(value);
							checkPerList.add(currentIndex, value);
						}

						break;
					}
				}
			}

			/*
			 * for (CostCoverDTO checkBean : listCheck) { String checkMonth =
			 * checkBean.getDate(); BigDecimal checkValue =
			 * checkBean.getCheckPer();
			 * 
			 * if (month.equals(checkMonth)) {
			 * 
			 * if (null != checkValue) { costCoverVO.setCheckPer(checkValue);
			 * checkPerList.add(orderMap.get(checkMonth),
			 * checkValue.doubleValue());
			 * 
			 * } else { costCoverVO.setCheckPer(new BigDecimal(0));
			 * checkPerList.add(orderMap.get(checkMonth), 0d); }
			 * 
			 * break; } }
			 */

			// 获取其他占比
			BigDecimal medicalValue = costCoverVO.getMedicalPer();
			BigDecimal opertionValue = costCoverVO.getOperationPer();
			BigDecimal checkValue = costCoverVO.getCheckPer();

			if (medicalValue == null) {
				medicalValue = new BigDecimal(0);
			}
			if (opertionValue == null) {
				opertionValue = new BigDecimal(0);
			}
			if (checkValue == null) {
				checkValue = new BigDecimal(0);
			}
			BigDecimal currentValue = new BigDecimal(100);
			BigDecimal otherValue = currentValue.subtract(medicalValue.add(opertionValue).add(checkValue));

			otherPerList.add(otherValue);
			costCoverVO.setOtherPer(otherValue);

			costCoverVOList.add(costCoverVO);

		}

		BigDecimal[] medical = medicalPerList.toArray(new BigDecimal[0]);
		BigDecimal[] opertion = opertionPerList.toArray(new BigDecimal[0]);
		BigDecimal[] check = checkPerList.toArray(new BigDecimal[0]);
		BigDecimal[] other = otherPerList.toArray(new BigDecimal[0]);

		String[] monthArray = monthRang.toArray(new String[0]);

		renturnBean.setCheckPer(check);
		renturnBean.setOperationPer(opertion);
		renturnBean.setMedicalPer(medical);
		renturnBean.setDate(monthArray);
		renturnBean.setOtherPer(other);

		renturnBean.setCostList(costCoverVOList);

		return renturnBean;

	}
}
